import 'package:flutter/material.dart';
import '../demo/ui_demo.dart';
import 'package:scoped_model/scoped_model.dart';
import '../local/model/app_model.dart';
class MyDemo extends StatelessWidget implements Demo {
  MyDemo(this.title);
  @override
  String title;
  @override
  Widget build(BuildContext context) {
    return new HomePage(title: this.title,);
  }
}

class HomePage extends StatelessWidget {
  String title;
  HomePage({this.title});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(this.title),
        actions: <Widget>[
          IconSource("ScopedModel","17"),
        ],
      ),
      body: StateDemo(),
    );
  }
}

class CounterModel extends Model {
   List<String> _fruits = ['apple', 'oragen', 'banana'];
   void onFresh () {
    _fruits = ['apple', 'oragen', 'banana'];
    notifyListeners();
  }
  void deleteItem(value) {
    _fruits.remove(value);
    notifyListeners();
  }
  List<String> get fruits => _fruits;
   static CounterModel of(BuildContext context,[bool rebuildOnChange]){
     if(rebuildOnChange!=null)
        return ScopedModel.of(context,rebuildOnChange: rebuildOnChange);
      return ScopedModel.of(context);
   }
}

class StateDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ScopedModel(
      model: CounterModel(),
      child: Scaffold(
        body: Container(
          child: Center(
            child: CounterWrapper(),
          ),
        ),
        floatingActionButton: ScopedModelDescendant<CounterModel>(
          rebuildOnChange: false,
          builder: (context, _, model) {
            return FloatingActionButton(
              onPressed: model.onFresh,
              child: Icon(Icons.refresh),
            );
          },
        ),
      ),
    );
  }
}

class CounterWrapper extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: CounterDemo(),
    );
  }
}

class CounterDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text("项目名称："+BaseModel.of<AppModel>(context).get("appName")),
          ScopedModelDescendant<CounterModel>(
            builder: (context, _, model) {
              return Wrap(
                spacing: 10.0,
                children: model.fruits.map((item) {
                  return Chip(
                    label: Text("${item}"),
                    onDeleted: () {
                      model.deleteItem(item);
                    },
                  );
                }).toList(),
              );
            },
          ),
          Divider(
            height: 18.0,
          ),
         ScopedModelDescendant<CounterModel>(
           builder: (context,_,model){
             return  OutlineButton(
               onPressed: () {
                 model.onFresh();
               },
               child: Text("刷新1"),
             );
           },
         ),
          OutlineButton(
            onPressed: () {
              CounterModel.of(context).onFresh();
            },
            child: Text("刷新2"),
          )
        ],
      ),
    );
  }
}
